import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class SplashMenu extends Canvas
{
	private MenuItem[] menuList;
	private int current;
	private MenuManager menuMgr;
	private int width;
	private int height;
	private boolean rpaint;	
		
	public SplashMenu(MenuItem[] list, MenuManager mngr)
	{
		this.menuMgr = mngr;
		this.menuList = list;
		this.current = menuMgr.getMainSelected();
		this.width = this.getWidth();
		this.height = this.getHeight();
		this.rpaint = false;
	}
	
	protected void keyRepeated(int keyCode)
	{
		keyReleased(keyCode);
	}
	
	protected void keyReleased(int keyCode)
	{
		if(rpaint)
			return;
			
		if(getGameAction(keyCode) == Canvas.FIRE || keyCode == Canvas.KEY_NUM5)
		{
			menuMgr.itemSelected(current);
			return;
		}
			
		if(getGameAction(keyCode) == Canvas.RIGHT || keyCode == Canvas.KEY_NUM6 ||
			getGameAction(keyCode) == Canvas.DOWN || keyCode == Canvas.KEY_NUM8)
			{
				/* Next item */
				if(this.current < this.menuList.length - 1)
					this.current++;
				else
					this.current = 0;
			}
		else if(getGameAction(keyCode) == Canvas.LEFT || keyCode == Canvas.KEY_NUM4 ||
			getGameAction(keyCode) == Canvas.UP || keyCode == Canvas.KEY_NUM2)
			{
				/* Next item */
				if(this.current > 0)
					this.current--;
				else
					this.current = this.menuList.length - 1;
			}
		menuMgr.mainSelected(this.current);
		repaint();
	}
	
	public void paint(Graphics g)
	{
		rpaint = true;
		g.setColor(menuMgr.getDefaultBGColor());
		g.fillRect(0,0, width, height);
		
		/* La oposité */
		g.setColor(0x00FFFFFF - g.getColor());
		
		/* Mini map */
		g.drawString((current + 1) + "/" + menuList.length, width / 2, 20, Graphics.BASELINE | Graphics.HCENTER);
	
		/* Left n' right */
		int l = 0;
		if(current > 0)
			l = current -1;
		else
			l = menuList.length - 1;
			
		g.drawImage(menuList[l].getIcon(), (width / 2) - (menuList[current].getIcon().getWidth() / 2) - 40, 
				height / 2, Graphics.VCENTER | Graphics.HCENTER);
		
		int r = 0;
		if(current < menuList.length -1)
			r = current + 1;
		else
			r = 0;
			
		g.drawImage(menuList[r].getIcon(), (width / 2) + (menuList[current].getIcon().getWidth() / 2) + 40, 
				height / 2, Graphics.VCENTER | Graphics.HCENTER);
				
		/* Draw current in the middle of screen */
		g.drawImage(menuList[current].getIcon(), width / 2, height / 2, Graphics.VCENTER | Graphics.HCENTER);
		
		/* Draw description below */
		g.drawString(menuList[current].getDescription(), width / 2, height - 20, Graphics.BASELINE | Graphics.HCENTER );
		rpaint = false;		
	}
}
